﻿using System;
using GeoAPI.Geometries;
using YJKGridTopologyCal.Geometries;
using YJKGridTopologyCal.Planargraph;

namespace YJKGridTopologyCal.Operation.Linemerge
{
	public class LineMergeGraph : PlanarGraph
	{
		public void AddEdge(ILineString lineString)
		{
			if (lineString.IsEmpty)
			{
				return;
			}
			Coordinate[] array = CoordinateArrays.RemoveRepeatedPoints(lineString.Coordinates);
			if (array.Length < 2)
			{
				return;
			}
			Coordinate coordinate = array[0];
			Coordinate coordinate2 = array[array.Length - 1];
			Node node = this.GetNode(coordinate);
			Node node2 = this.GetNode(coordinate2);
			DirectedEdge de = new LineMergeDirectedEdge(node, node2, array[1], true);
			DirectedEdge de2 = new LineMergeDirectedEdge(node2, node, array[array.Length - 2], false);
			Edge edge = new LineMergeEdge(lineString);
			edge.SetDirectedEdges(de, de2);
			base.Add(edge);
		}

		private Node GetNode(Coordinate coordinate)
		{
			Node node = base.FindNode(coordinate);
			if (node == null)
			{
				node = new Node(coordinate);
				base.Add(node);
			}
			return node;
		}
	}
}
